﻿function ss_open_author_list(that) {
    if ($(that).parent().parent().hasClass("opened")) {
        $(that).removeClass("fa-rotate-90");
        $(that).parent().parent().find(".ss-author-member-list").animate({ height: "toggle" }, "fast");
        $(that).parent().parent().removeClass("opened");
    }
    else {
        $(that).addClass("fa-rotate-90");
        $(that).parent().parent().addClass("opened");
        $(that).parent().parent().find(".ss-author-member-list").animate({ height: "toggle" }, "fast");
    }
}
$(document).ready(function () {
    $(".ss-menu-link").click(function () {
        $("#ss-edit-menu-form").css("display", "block");
        $("#ss-create-menu-form").css("display", "none");
    })
    $(".ss-create-menu-btn").click(function () {
        $("#ss-create-menu-form").css("display", "block");
        $("#ss-edit-menu-form").css("display", "none");
    })
})

function edit_menu(id) {
    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            document.getElementById("ss-edit-menu-form").innerHTML = xhttp.responseText;
            $(document).ready(function () {
                $("#txt_edit_menu_icon").on("change", function () {
                    var icon = document.getElementById("txt_edit_menu_icon").value;
                    if (icon != "") {
                        $("#edit_menu_icon").removeClass().addClass(icon);
                    }
                    else $("#edit_menu_icon").removeClass().addClass("fa fa-home");
                })

            });
        }
    };
    xhttp.open("GET", "../admin/systemsettings/control/ajax/menu/menu_edit_form.aspx?id=" + id, true);
    xhttp.send();
}
function btn_edited_menu(id) {
    var parent = document.getElementById("ddl_edit_menu").value;
    var name = document.getElementById("txt_edit_menu_name").value;
    var icon = document.getElementById("txt_edit_menu_icon").value;
    var url = document.getElementById("txt_edit_menu_url").value;
    var routing = document.getElementById("txt_edit_menu_routing").value;
    var update = 0;
    if ($("#isupdate2").is(":checked"))
        update = 1;
    if (name.trim() == "") {
        document.getElementById("error-modal").style.display = "none";
        document.getElementById("error-modal").style.display = "block";
        document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Tên danh mục không được để trống!";
        setTimeout("$('#error-modal').fadeOut()", 2000);
    }
    else {
        if (icon.trim() == "") {
            document.getElementById("error-modal").style.display = "none";
            document.getElementById("error-modal").style.display = "block";
            document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Biểu tượng không được để trống!";
            setTimeout("$('#error-modal').fadeOut()", 2000);
        } else {
            if (url.trim() == "" && parent!=0) {
                document.getElementById("error-modal").style.display = "none";
                document.getElementById("error-modal").style.display = "block";
                document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Đường dẫn không được để trống!";
                setTimeout("$('#error-modal').fadeOut()", 2000);
            }
            else {
                if (routing.trim() == "" && parent != 0) {
                    document.getElementById("error-modal").style.display = "none";
                    document.getElementById("error-modal").style.display = "block";
                    document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Tên đường dẫn không được để trống!";
                    setTimeout("$('#error-modal').fadeOut()", 2000);
                }
                else {
                    if (parent != 0) {
                        var xhttp1;
                        xhttp1 = new XMLHttpRequest();
                        xhttp1.onreadystatechange = function () {
                            if (xhttp1.readyState == 4 && xhttp1.status == 200) {
                                document.getElementById("check_child").innerHTML = xhttp1.responseText;
                                var child = document.getElementById("check_child").innerHTML.trim();

                                if (parseInt(child) != 0) {
                                    document.getElementById("error-modal").style.display = "none";
                                    document.getElementById("error-modal").style.display = "block";
                                    document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Tồn tại danh mục con trong danh mục này!";
                                    setTimeout("$('#error-modal').fadeOut()", 2000);
                                } else {
                                    var xhttp;
                                    xhttp = new XMLHttpRequest();
                                    xhttp.onreadystatechange = function () {
                                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                                            location.reload(true);
                                        }
                                    };
                                    xhttp.open("GET", "../admin/systemsettings/control/ajax/menu/menu_edited.aspx?parent=" + parent + "&name=" + name + "&icon=" + icon + "&url=" + url + "&routing=" + routing + "&update=" + update + "&id=" + id, true);
                                    xhttp.send();
                                }
                            }
                        };
                        xhttp1.open("GET", "../admin/systemsettings/control/ajax/menu/check_menu_child.aspx?id=" + id, true);
                        xhttp1.send();
                    }
                    else {
                        var xhttp;
                        xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                location.reload(true);
                            }
                        };
                        xhttp.open("GET", "../admin/systemsettings/control/ajax/menu/menu_edited.aspx?parent=" + parent + "&name=" + name + "&icon=" + icon + "&url=" + url + "&routing=" + routing + "&update=" + update + "&id=" + id, true);
                        xhttp.send();
                    }
                }
            }
        }
    }

}

function btn_new_menu() {
    var parent = document.getElementById("ddl_new_menu").value;
    var name = document.getElementById("txt_new_menu_name").value;
    var icon = document.getElementById("txt_new_menu_icon").value;
    var url = document.getElementById("txt_new_menu_url").value;
    var routing = document.getElementById("txt_new_menu_routing").value;
    var update = 0;
    if ($("#isupdate").is(":checked"))
        update = 1;

    if (name.trim() == "") {
        document.getElementById("error-modal").style.display = "none";
        document.getElementById("error-modal").style.display = "block";
        document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Tên danh mục không được để trống!";
        setTimeout("$('#error-modal').fadeOut()", 2000);
    }
    else {
        if (icon.trim() == "") {
            document.getElementById("error-modal").style.display = "none";
            document.getElementById("error-modal").style.display = "block";
            document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Biểu tượng không được để trống!";
            setTimeout("$('#error-modal').fadeOut()", 2000);
        } else {
            if (url.trim() == "" && parent!=0) {
                document.getElementById("error-modal").style.display = "none";
                document.getElementById("error-modal").style.display = "block";
                document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Đường dẫn không được để trống!";
                setTimeout("$('#error-modal').fadeOut()", 2000);
            }
            else {
                if (routing.trim() == "" && parent!=0) {
                    document.getElementById("error-modal").style.display = "none";
                    document.getElementById("error-modal").style.display = "block";
                    document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Tên đường dẫn không được để trống!";
                    setTimeout("$('#error-modal').fadeOut()", 2000);
                }
                else {
                    var xhttp;
                    xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function () {
                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                            location.reload(true);
                        }
                    };
                    xhttp.open("GET", "../admin/systemsettings/control/ajax/menu/add_new_menu.aspx?parent=" + parent + "&name=" + name + "&icon=" + icon + "&url=" + url + "&routing=" + routing + "&update=" + update, true);
                    xhttp.send();
                }
            }
        }
    }
}

function btn_del_menu(id) {
    document.getElementById("del-modal").style.display = "block";
    document.getElementById("del_title").innerHTML = "Danh mục này sẽ bị xóa?";
    document.getElementById("delete_id").innerHTML = id;
}

function btn_delete() {
    var id = document.getElementById("delete_id").innerHTML;

    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            location.reload(true);
        }
    };
    xhttp.open("GET", "../admin/systemsettings/control/ajax/menu/menu_delete.aspx?id=" + id, true);
    xhttp.send();
}
$(document).ready(function () {
    $("#txt_new_menu_icon").on("change", function () {
        var icon = document.getElementById("txt_new_menu_icon").value;
        if (icon != "") {
            $("#new_menu_icon").removeClass().addClass(icon);
        }
        else $("#new_menu_icon").removeClass().addClass("fa fa-home");
    })
})
